//
//  TEDatabaseTest.m
//  TintExample
//
//  Created by Kevin Wu on 9/4/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TEDatabaseTest.h"

@implementation TEDatabaseTest

TTSynthesizeSingleton(TEDatabaseTest);


#pragma mark - Accessors

@synthesize inserted = _inserted;
@synthesize updated = _updated;
@synthesize deleted = _deleted;


#pragma mark - Memory

- (id)init
{
    self = [super init];
    if ( self ) {
        
        NSString *path = [TTDocumentsPath() stringByAppendingPathComponent:@"database.sqlite"];
        _db = [[TTDatabase alloc] initWithPath:path];
        [_db open];
        
        // aint INTEGER PRIMARY KEY, 
        // abool INTEGER, 
        // alonglong INTEGER, 
        // adouble REAL, 
        // astring TEXT, 
        // adate TEXT
        
        if ( ![_db hasTable:@"smpl"] ) {
            [_db executeUpdateString:@"CREATE TABLE smpl(aint INTEGER PRIMARY KEY, abool INTEGER, alonglong INTEGER, adouble REAL, astring TEXT, adate TEXT);"];
        }
        
        
        
        _inserted = 0;
        _updated = 0;
        _deleted = 0;
        
    }
    return self;
}

- (void)dealloc
{
    TTRelease(_db);
    
    _inserted = 0;
    _updated = 0;
    _deleted = 0;
    [super dealloc];
}


#pragma mark - Public

- (void)insertEntry1:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    while ( YES ) {
        if ( !_stop ) {
            
            // aint INTEGER PRIMARY KEY, 
            // abool INTEGER, 
            // alonglong INTEGER, 
            // adouble REAL, 
            // astring TEXT, 
            // adate TEXT
            
            if ( _inserted > 100000 ) {
                continue;
            }
            
            
            _inserted++;
            NSString *sql = @"INSERT INTO smpl(abool, alonglong, adouble, astring, adate) VALUES(?, ?, ?, ?, ?);";
            
            [_db executeInsert:sql, 
             [NSNumber numberWithBool:arc4random()%2], 
             [NSNumber numberWithInt:arc4random()%10000], 
             [NSNumber numberWithLongLong:arc4random()], 
             [NSString randStringWithLength:arc4random()%64+32], 
             [[NSDate date] dateByAddingSeconds:arc4random()%(TTTimeIntervalWeek()*1000)], nil];
             
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
            }
            
            
        }
        
        //[NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}

- (void)insertEntry2:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    while ( YES ) {
        if ( !_stop ) {
            
            // aint INTEGER PRIMARY KEY, 
            // abool INTEGER, 
            // alonglong INTEGER, 
            // adouble REAL, 
            // astring TEXT, 
            // adate TEXT
            
            if ( _inserted > 100000 ) {
                continue;
            }
            
            _inserted++;
            NSString *sql = @"INSERT INTO smpl(abool, alonglong, adouble, astring, adate) VALUES(?, ?, ?, ?, ?);";
            
            [_db executeInsert:sql, 
             [NSNumber numberWithBool:arc4random()%2], 
             [NSNumber numberWithInt:arc4random()%10000], 
             [NSNumber numberWithLongLong:arc4random()], 
             [NSString randStringWithLength:arc4random()%64+32], 
             [[NSDate date] dateByAddingSeconds:arc4random()%(TTTimeIntervalWeek()*1000)], nil];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
            }
            
            
        }
        
        //[NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}


- (void)updateEntry1:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    TTDatabaseResult *result = nil;
    
    while ( YES ) {
        if ( !_stop ) {
            
            // aint INTEGER PRIMARY KEY, 
            // abool INTEGER, 
            // alonglong INTEGER, 
            // adouble REAL, 
            // astring TEXT, 
            // adate TEXT
            
            if ( (result == nil) || ([result count] < 1) ) {
                TTRelease(result);
                result = [[_db executeQueryString:@"SELECT * FROM smpl;"] retain];
                if ( [result count] < 1 ) {
                    continue;
                }
            }
            
            id row = [result.rows randomObject];
            
            _updated++;
            NSString *sql = @"UPDATE smpl SET alonglong=?, adouble=?, astring=?, adate=? WHERE aint=?;";
            [_db executeUpdate:sql, 
             [NSNumber numberWithInt:arc4random()%10000], 
             [NSNumber numberWithLongLong:arc4random()], 
             [NSString randStringWithLength:arc4random()%64+32], 
             [[NSDate date] dateByAddingSeconds:arc4random()%(TTTimeIntervalWeek()*1000)], 
             [NSNumber numberWithInt:[row intForColumn:@"aint"]], nil];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
                
                TTRelease(result);
                
            }
            
            
        }
        
        //[NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}

- (void)updateEntry2:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    TTDatabaseResult *result = nil;
    
    while ( YES ) {
        if ( !_stop ) {
            
            // aint INTEGER PRIMARY KEY, 
            // abool INTEGER, 
            // alonglong INTEGER, 
            // adouble REAL, 
            // astring TEXT, 
            // adate TEXT
            
            if ( (result == nil) || ([result count] < 1) ) {
                TTRelease(result);
                result = [[_db executeQueryString:@"SELECT * FROM smpl;"] retain];
                if ( [result count] < 1 ) {
                    continue;
                }
            }
            
            id row = [result.rows randomObject];
            
            _updated++;
            NSString *sql = @"UPDATE smpl SET alonglong=?, adouble=?, astring=?, adate=? WHERE aint=?;";
            [_db executeUpdate:sql, 
             [NSNumber numberWithInt:arc4random()%10000], 
             [NSNumber numberWithLongLong:arc4random()], 
             [NSString randStringWithLength:arc4random()%64+32], 
             [[NSDate date] dateByAddingSeconds:arc4random()%(TTTimeIntervalWeek()*1000)], 
             [NSNumber numberWithInt:[row intForColumn:@"aint"]], nil];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
                
                TTRelease(result);
                
            }
            
            
        }
        
        //[NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}


- (void)deleteEntry1:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    TTDatabaseResult *result = nil;
    
    while ( YES ) {
        if ( !_stop ) {
            
            // aint INTEGER PRIMARY KEY, 
            // abool INTEGER, 
            // alonglong INTEGER, 
            // adouble REAL, 
            // astring TEXT, 
            // adate TEXT
            
            if ( (result == nil) || ([result count] < 1) ) {
                TTRelease(result);
                result = [[_db executeQueryString:@"SELECT * FROM smpl;"] retain];
                if ( [result count] < 1 ) {
                    continue;
                }
            }
            
            id row = [result.rows randomObject];
            
            _deleted++;
            NSString *sql = @"DELETE FROM smpl WHERE aint=?;";
            [_db executeUpdate:sql, [NSNumber numberWithInt:[row intForColumn:@"aint"]], nil];
            [result.rows removeObject:row];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
                
                TTRelease(result);
                
            }
            
            
        }
        
        //[NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}

- (void)deleteEntry2:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    TTDatabaseResult *result = nil;
    
    while ( YES ) {
        if ( !_stop ) {
            
            // aint INTEGER PRIMARY KEY, 
            // abool INTEGER, 
            // alonglong INTEGER, 
            // adouble REAL, 
            // astring TEXT, 
            // adate TEXT
            
            if ( (result == nil) || ([result count] < 1) ) {
                TTRelease(result);
                result = [[_db executeQueryString:@"SELECT * FROM smpl;"] retain];
                if ( [result count] < 1 ) {
                    continue;
                }
            }
            
            id row = [result.rows randomObject];
            
            _deleted++;
            NSString *sql = @"DELETE FROM smpl WHERE aint=?;";
            [_db executeUpdate:sql, [NSNumber numberWithInt:[row intForColumn:@"aint"]], nil];
            [result.rows removeObject:row];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
                
                TTRelease(result);
                
            }
            
            
        }
        
        //[NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}



#pragma mark - Override


- (void)startTest
{
    [super startTest];
    
    if ( !_launched ) {
        _launched = YES;
        [self performSelectorInBackground:@selector(insertEntry1:) withObject:nil];
        //[self performSelectorInBackground:@selector(insertEntry2:) withObject:nil];
        [self performSelectorInBackground:@selector(updateEntry1:) withObject:nil];
        //[self performSelectorInBackground:@selector(updateEntry2:) withObject:nil];
        [self performSelectorInBackground:@selector(deleteEntry1:) withObject:nil];
        //[self performSelectorInBackground:@selector(deleteEntry2:) withObject:nil];
    }
}


@end
